
****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

use all-lottery-movers-analysis.dta , clear

merge m:1 appch2_link year using app2choices.dta, gen(app2mg)
keep if (app2mg==1 | app2mg==3)
merge m:1 appch3_link year using app3choices.dta, gen(app3mg)
keep if (app3mg==1 | app3mg==3)

gen attappch1schl = (curr_lead1==appch1school)
gen attappch2schl = (curr_lead1==appch2school)
gen attappch3schl = (curr_lead1==appch3school)
gen attassschl = (curr_lead1==assschl)

xtset lottery
	
tempfile alllottery
	save `alllottery'
	
keep if ksample	
keep if year>2008 	
	
estpost sum attassschl if ksample==1
su attassschl if (ksample==1 & wonfirst==1)
estadd scalar R1 = r(N)
su attassschl if (ksample==1 & wonfirst==1 & attassschl==1 & changehs==1)
estadd scalar R2 = r(N)
su attassschl if (ksample==1 & wonfirst==1 & attassschl==1 & changehs==0)
estadd scalar R3 = r(N)
su attassschl if (ksample==1 & wonfirst==1 & attassschl==0 & move1==1)
estadd scalar R4 = r(N)	
su attassschl if (ksample==1 & wonfirst==1 & attassschl==0 & move1==0)
estadd scalar R5 = r(N)		
su attassschl if (ksample==1 & wonfirst==1 & attappch2schl==1)	
estadd scalar R13 = r(N)
su attassschl if (ksample==1 & wonfirst==1 & attappch3schl==1)	
estadd scalar R14 = r(N)

su attassschl if (ksample==1 & wonfirst==0)
estadd scalar R6 = r(N)	
su attassschl if (ksample==1 & wonfirst==0 & move1==1)
estadd scalar R7 = r(N)	
su attassschl if (ksample==1 & wonfirst==0 & move1==1 & changehs==0)
estadd scalar R8 = r(N)	
su attassschl if (ksample==1 & wonfirst==0 & changehs==1)
estadd scalar R9 = r(N)		
su attassschl if (ksample==1 & wonfirst==0 & move1==0)
estadd scalar R10 = r(N)		
su attassschl if (ksample==1 & wonfirst==0 & attappch2schl==1)	
estadd scalar R11 = r(N)
su attassschl if (ksample==1 & wonfirst==0 & attappch3schl==1)	
estadd scalar R12 = r(N)

est store A
	
estpost sum attassschl if ksample==1
local total = e(N)
su attassschl if (ksample==1 & wonfirst==1)
estadd scalar R1 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==1 & attassschl==1 & changehs==1)
estadd scalar R2 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==1 & attassschl==1 & changehs==0)
estadd scalar R3 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==1 & attassschl==0 & move1==1)
estadd scalar R4 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==1 & attassschl==0 & move1==0)
estadd scalar R5 = (r(N)	 / `total' )	
su attassschl if (ksample==1 & wonfirst==1 & attappch2schl==1)	
estadd scalar R13 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==1 & attappch3schl==1)	
estadd scalar R14 = (r(N) / `total' )

su attassschl if (ksample==1 & wonfirst==0)
estadd scalar R6 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==0 & move1==1)
estadd scalar R7 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==0 & move1==1 & changehs==0)
estadd scalar R8 = (r(N) / `total' )	
su attassschl if (ksample==1 & wonfirst==0 & changehs==1)
estadd scalar R9 = (r(N) / `total' )	
su attassschl if (ksample==1 & wonfirst==0 & move1==0)
estadd scalar R10 = (r(N) / `total' )		
su attassschl if (ksample==1 & wonfirst==0 & attappch2schl==1)	
estadd scalar R11 = (r(N) / `total' )
su attassschl if (ksample==1 & wonfirst==0 & attappch3schl==1)	
estadd scalar R12 = (r(N) / `total' )

est store B

	
use `alllottery' , clear
keep if sixsample	
keep if year>2008 
	
estpost sum attassschl if sixsample==1
su attassschl if (sixsample==1 & wonfirst==1)
estadd scalar R1 = r(N)
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==1 & changehs==1)
estadd scalar R2 = r(N)
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==1 & changehs==0)
estadd scalar R3 = r(N)
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==0 & move1==1)
estadd scalar R4 = r(N)	
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==0 & move1==0)
estadd scalar R5 = r(N)	
su attassschl if (sixsample==1 & wonfirst==1 & attappch2schl==1)	
estadd scalar R13 = r(N)
su attassschl if (sixsample==1 & wonfirst==1 & attappch3schl==1)	
estadd scalar R14 = r(N)
	
su attassschl if (sixsample==1 & wonfirst==0)
estadd scalar R6 = r(N)	
su attassschl if (sixsample==1 & wonfirst==0 & move1==1)
estadd scalar R7 = r(N)	
su attassschl if (sixsample==1 & wonfirst==0 & move1==1 & changehs==0)
estadd scalar R8 = r(N)	
su attassschl if (sixsample==1 & wonfirst==0 & changehs==1)
estadd scalar R9 = r(N)		
su attassschl if (sixsample==1 & wonfirst==0 & move1==0)
estadd scalar R10 = r(N)	
su attassschl if (sixsample==1 & wonfirst==0 & attappch2schl==1)	
estadd scalar R11 = r(N)
su attassschl if (sixsample==1 & wonfirst==0 & attappch3schl==1)	
estadd scalar R12 = r(N)	
	
est store C

estpost sum attassschl if sixsample==1
local total = e(N)
su attassschl if (sixsample==1 & wonfirst==1)
estadd scalar R1 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==1 & changehs==1)
estadd scalar R2 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==1 & changehs==0)
estadd scalar R3 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==0 & move1==1)
estadd scalar R4 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==1 & attassschl==0 & move1==0)
estadd scalar R5 = (r(N)	 / `total' )	
su attassschl if (sixsample==1 & wonfirst==1 & attappch2schl==1)	
estadd scalar R13 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==1 & attappch3schl==1)	
estadd scalar R14 = (r(N) / `total' )

su attassschl if (sixsample==1 & wonfirst==0)
estadd scalar R6 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==0 & move1==1)
estadd scalar R7 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==0 & move1==1 & changehs==0)
estadd scalar R8 = (r(N) / `total' )	
su attassschl if (sixsample==1 & wonfirst==0 & changehs==1)
estadd scalar R9 = (r(N) / `total' )	
su attassschl if (sixsample==1 & wonfirst==0 & move1==0)
estadd scalar R10 = (r(N) / `total' )		
su attassschl if (sixsample==1 & wonfirst==0 & attappch2schl==1)	
estadd scalar R11 = (r(N) / `total' )
su attassschl if (sixsample==1 & wonfirst==0 & attappch3schl==1)	
estadd scalar R12 = (r(N) / `total' )

est store D

xtreg mathstand_as move1
est store empty

** Table A2
esttab A B empty C D empty using tableA2.tex , replace ///
mtitles("Count" "Percent of Sample" " " "Count" "Percent of Sample") ///
cells(mean(fmt(2)) sd(fmt(2)par)) label booktabs nonum collabels(none) gaps f ///
stats(R1 R2 R3 R4 R5 R13 R14 R6 R7 R8 R9 R10 R11 R12, ///
fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f) ///
layout( ) ///
labels(`"Winners"' `"Complier, Move"' `"Complier, Stay"' `"Non-Complier, Move"' `"Non-Complier, Stay"' `"Attend 2nd Choice"' `"Attend 3rd Choice"' `"Losers"' `"Move"' `"Exit CMS"' `"Move in CMS"' `"Stay"' `"Attend 2nd Choice"' `"Attend 3rd Choice"')) 




